package cn.zhage.aimin.auth1.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.SaLoginModel;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.zhage.aimin.auth1.common.Result;
import cn.zhage.aimin.auth1.dto.UserDTO;
import cn.zhage.aimin.auth1.entity.User;
import cn.zhage.aimin.auth1.service.UserService;
import cn.zhage.aimin.satoken1.admin.AdminStpUtil;
import cn.zhage.aimin.satoken1.user.UserStpUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.Permission;
import java.util.List;

/*
 *
 * @author 渣哥
 */
@RestController
@RequestMapping("/test")
@RequiredArgsConstructor
public class TestController {

    private final RedisTemplate<String, Object> redisTemplate;

    private final UserService userService;

    private final RedisConnectionFactory redisConnectionFactory;

    @GetMapping("/test")
    public String test(HttpServletRequest request) {
        // 检查权限
        String token = request.getHeader("token");
        try{
            StpUtil.checkLogin();
        }catch (Exception e){
            return "error";
        }
        return "success";
    }

    @GetMapping("/getAll")
    public String getAll(){

        return userService.list().toString();
    }

    @GetMapping("/join")
    public String join(){
        List<UserDTO> list = userService.getUserListJoinAddress(2);
        for (Object user : list) {
            System.out.println(user);
        }
        return "success";
    }

    @GetMapping("/join2")
    public String join2(){
        List<User> list = userService.listDeep(Wrappers.emptyWrapper());
        list.forEach(System.out::println);
        //User byId = userService.getById(1);
        return "success";
    }

    @GetMapping("/delete")
    public String delete(){
        userService.removeById(1);
        return "success";
    }

    @GetMapping("/page")
    public String page(){
        int pageNo = 1;
        int pageSize = 10;
        Page<User> page = userService.page(new Page<>(pageNo, pageSize), Wrappers.emptyWrapper());
        List<User> records = page.getRecords();
        records.forEach(System.out::println);
        return "success";
    }

    @GetMapping("/redis")
    public String redis(){
        redisTemplate.opsForValue().set("name", "zhangsan");
        return "success";
    }

    @GetMapping("/redisConnection")
    public String redisConnection(){

        if (redisConnectionFactory instanceof LettuceConnectionFactory) {
            LettuceConnectionFactory lettuceFactory = (LettuceConnectionFactory) redisConnectionFactory;
            LettuceClientConfiguration clientConfig = lettuceFactory.getClientConfiguration();

            // 查看是否启用了池
            if (clientConfig instanceof LettucePoolingClientConfiguration) {
                System.out.println("Redis 使用了连接池!");
                // 可以进一步检查池配置
            } else {
                System.out.println("Redis 没有使用连接池!");
            }
        }

        return "success";
    }



    // 测试user用户 登录/验证登录
    @GetMapping("/login")
    public String login(){
        SaLoginModel saLoginModel = new SaLoginModel();
        saLoginModel.setIsWriteHeader(false);
        StpUtil.login(1,saLoginModel);
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        return tokenInfo.getTokenValue();
    }


    @GetMapping("/checkLogin")
    public String checkLogin(){
        return "success";
    }


    // 测试user用户 登录/验证登录
    @GetMapping("/userLogin")
    public String userLogin(){
        SaTokenInfo tokenInfo = UserStpUtil.miniLogin("3");
        return tokenInfo.getTokenValue();
    }

    @GetMapping("/userLoginCheck")
    public String userLoginCheck(){
      return "userLoginCheck：success";
    }

    // 测试user用户 登录/验证登录
    @GetMapping("/adminLogin")
    public String adminLogin(){
        SaTokenInfo tokenInfo = AdminStpUtil.login("23");
        return tokenInfo.getTokenValue();
    }

    @GetMapping("/adminLoginCheck")
    public String adminLoginCheck(){
        return "userLoginCheck：success";
    }








    @GetMapping("/permission1")
    @SaCheckPermission("user:add")
    public String permission1(){
        //1. 获取用户 关于satoken 是如何获取用户信息的
            //jwt 解析token就能获取用户信息
            // sokentoken 是基于Redis实现的用户信息保存，所以可以直接通过token 获取用户唯一标识
        //2. 根据用户ID 查询全部权限
            // 用户登录成功之后，直接查询出全部权限来，保存到redis/内存中
        //3. 全部权限跟当前权限进行对比

        UserStpUtil.stpLogic.checkPermission("permission1");
        UserStpUtil.stpLogic.checkRole("admin");


        return "success";
    }


}
